﻿using GameEngine.Model;
using GameEngine.Model.Data;
using System;
using System.Collections.Generic;
using System.Windows;

namespace MySettlers.Things
{
    public class PeasantVO : MovingThingVO<int>
    {
        public bool Working { get; set; }

        public PeasantVO() 
        {
        }

        public PeasantVO(IThingVOId<int> id, IThingTypeVOId typeId) :
            base(id, typeId)
        {
            //ClassType = "PeasantVO";
            //Eating = false;
            Working = false;
        }
    }

    public class PeasantDataProxy : MovingThingDataProxy<int>
    {
        //public bool Eating
        //{
        //    get { return VO.Eating; }
        //    set { VO.Eating = value; }
        //}

        public new PeasantVO VO
        {
            get { return this.Data as PeasantVO; }
        }

        public PeasantDataProxy(
            PeasantVO item
            )
            : base(item)
        {

        }

        protected DateTime startedWorking;

        long workTime = 5000;

        public override Point Direction
        {
            set
            {
                base.Direction = value;
                UpdateState();
            }
        }

        public override Point Position
        {
            set
            {
                Point newPosition = base.Position;
                if (value.X != base.Position.X)
                {
                    newPosition.X = value.X;
                }
                else
                    if (value.Y != base.Position.Y)
                        newPosition.Y = value.Y;
                base.Position = newPosition;
            }
            get {
                return base.Position;
            }
        }

        protected bool Working {
            get { return VO.Working; }
            set {
                VO.Working = value;
                UpdateState();
            }
        }

        override public void UpdateState() {
            if (Moving)
            {
                if (Direction.X > 0)
                {
                    State = MyFacade.Instance.walkEastState;
                }
                else if (Direction.X < 0)
                {
                    State = MyFacade.Instance.walkWestState;
                }
                else
                {
                    if (Direction.Y > 0)
                    {
                        State = MyFacade.Instance.walkNorthState;
                    }
                    else if (Direction.Y < 0)
                    {
                        State = MyFacade.Instance.walkSouthState;
                    }
                }
            }
            else {
                if (Direction.X > 0)
                {
                    //State = MySmurfsFacade.Instance.standEastState;
                    State = MyFacade.Instance.standSouthState;//default state
                }
                else if (Direction.X < 0)
                {
                    //State = MySmurfsFacade.Instance.standWestState;
                    State = MyFacade.Instance.standSouthState;
                }
                else
                {
                    if (Direction.Y > 0)
                    {
                        //State = MySmurfsFacade.Instance.standNorthState;
                        State = MyFacade.Instance.standSouthState;
                    }
                    else if (Direction.Y < 0)
                    {
                        State = MyFacade.Instance.standSouthState;
                    }
                }
            }
        }

        protected bool HasGoods;
        public static void Action(EngineDataProxy<int> engine, IWorldDataProxy<int> world, IThingTypeDataProxy<int> itemType, IThingDataProxy<int> item)
        {
            PeasantDataProxy thing = (PeasantDataProxy)item;
            PeasantTypeDataProxy thingType = (PeasantTypeDataProxy)itemType;
            if (thing.HasGoods)
            {
                if (!thing.Destination.HasValue)
                {
                    List<IThingDataProxy<int>> houses = new List<IThingDataProxy<int>>();
                    foreach (IThingDataProxy<int> otheritem in world.Items.Values)
                    {
                        if (otheritem.TypeId.Equals(MyFacade.Instance.houseTypeId))
                        {
                            houses.Add(otheritem);
                        }
                    }
                    //TODO ver a k está mais perto ou a dele proprio ou o castelo
                    thing.Destination = houses[0].Position;
                }
                else
                {
                    if (world.Map.DistanceInTiles(thing.Destination.Value, thing.Position) < 2)
                    {
                        thing.Destination = null;
                        thing.HasGoods = false;
                    }
                }
            }
            else
            {
                if (thing.Working)
                {
                    if (TimeSpan.FromTicks(DateTime.Now.Ticks).TotalMilliseconds - TimeSpan.FromTicks(thing.startedWorking.Ticks).TotalMilliseconds > thing.workTime)
                    {
                        thing.HasGoods = true;
                        thing.Working = false;
                    }
                }
                else
                {
                    if (!thing.Destination.HasValue)
                    {
                        List<IThingDataProxy<int>> fields = new List<IThingDataProxy<int>>();
                        foreach (IThingDataProxy<int> otheritem in world.Items.Values)
                        {
                            if (otheritem.TypeId.Equals(MyFacade.Instance.fieldTypeId))
                            {
                                if(!otheritem.MovingWithMouse)
                                    fields.Add(otheritem);
                            }
                        }
                        if(fields.Count>0)
                            thing.Destination = fields[0].Position;
                    }
                    else
                    {
                        if (world.Map.DistanceInTiles(thing.Destination.Value, thing.Position) < 2)
                        {
                            thing.Destination = null;
                            thing.Working = true;
                            thing.startedWorking = DateTime.Now;
                        }
                    }
                }
            }
        }
    }

    public class PeasantTypeVO : MovingThingTypeVO<int>
    {
        public PeasantTypeVO(IThingTypeVOId id, IViewVOId<int> view) :
            base(id)
        {
            ViewId = view;
            //Type = "PeasantTypeVO";
        }

        public PeasantTypeVO() { }
    }

    public class PeasantTypeDataProxy : MovingThingTypeDataProxy<int>
    {
        public PeasantTypeDataProxy(
            IMovingThingTypeVO<int> typeVO,
            IViewDataProxy<int> view)
            : base(typeVO, view)
        {
            AddSense("Visao", 5, GameEngine.Model.SensibilityRadiusEnum.forwardAndBack);
        }
    }
}
